% read xls file
clear all
close all
fclose all

path1='C:\Users\Tom�s\Desktop\Temp\';


fileName='CC.xlsx';

opts = detectImportOptions([path1 fileName]);
preview([path1 fileName],opts)


% opts = setvaropts(opts,'TreatAsMissing','nan');
% opts = setvaropts(opts,'TreatAsMissing','NaN');
opts.PreserveVariableNames=1
opts.MissingRule = 'fill';
% opts.TreatAsMissing='nan';
opts.VariableNamesRange=1;

% opts = setvaropts(opts,{'spikelets'},'FillValue','nan');
% opts = setvaropts(opts,'FillValue','NaN');



M = readcell([path1 fileName],'NumHeaderLines',1);
%load Soma locations of targets
for i=1:size(M,1)
    
temp=M{i,4};
spaces = strfind(temp,' ')
commas= strfind(temp,',')

if isempty(spaces)
    synapses(i,:)=nan
else
synapses(i,1)=str2num(temp(1:commas(1)-1));
synapses(i,2)=str2num(temp(spaces(1)+1:commas(2)-1));
synapses(i,3)=str2num(temp(spaces(2)+1:end));
end

end

% load synapse locations
for i=1:size(M,1)
    
%organize variable ID
temp=M{i,6};
spaces = strfind(temp,' ')
commas= strfind(temp,',')

if isempty(spaces)
    SomaLocations(i,:)=nan
else
SomaLocations(i,1)=str2num(temp(1:commas(1)-1));
SomaLocations(i,2)=str2num(temp(spaces(1)+1:commas(2)-1));
SomaLocations(i,3)=str2num(temp(spaces(2)+1:end));
end

end

% load target cell type
for i=1:size(M,1)
    if ~ischar(M{i,7})
    targetType(i)=M{i,7};
    else
        targetType(i)=nan;
    end

end
%make logicals for cell type to plot in different colors
l1=targetType==1; %for mli
l0=targetType==0; %for unknown dendrite mli-like
l2=targetType==2; %for PC
l3=targetType==3; %for Golgi cell
l4=targetType==4; %for pli

%define colors
c0=[242 192 116]/255;
c1=[249 161 27]/255;
c2=[130 0 159]/255;
c3=[0 159 0]/255;
c4=[0 0 159]/255;

CCColor=[30 180 233]/255


CCSoma=[140073 92518 1127];
pia=[119183 40082 670];
scaleFactor=253; %253 px/micron

%make plot
figure;
hold on
s=scatter(SomaLocations(l1,1)/scaleFactor,-SomaLocations(l1,2)/scaleFactor,60'.','filled')
s.MarkerFaceColor=c1;
s.MarkerFaceAlpha=0.5
s=scatter(SomaLocations(l2,1)/scaleFactor,-SomaLocations(l2,2)/scaleFactor,60'.','filled')
s.MarkerFaceColor=c2;
s.MarkerFaceAlpha=0.5
s=scatter(SomaLocations(l3,1)/scaleFactor,-SomaLocations(l3,2)/scaleFactor,60'.','filled')
s.MarkerFaceColor=c3;
s.MarkerFaceAlpha=0.5
s=scatter(SomaLocations(l4,1)/scaleFactor,-SomaLocations(l4,2)/scaleFactor,60'.','filled')
s.MarkerFaceColor=c4;
s.MarkerFaceAlpha=0.5
s=scatter(SomaLocations(l0,1)/scaleFactor,-SomaLocations(l0,2)/scaleFactor,60'.','filled')



scatter(CCSoma(1)/scaleFactor,-CCSoma(2)/scaleFactor,90'.','filled','MarkerFaceColor',[30 180 233]/255)
scatter(pia(1)/scaleFactor,-pia(2)/scaleFactor,60,'.','filled')


s=scatter(synapses(:,1)/scaleFactor,-synapses(:,2)/scaleFactor,60'.','filled')
s.MarkerFaceColor=CCColor;
s.MarkerFaceAlpha=0.9


axis equal
axis off
sb=scalebar('XUnit','�m','YUnit','�m','YLen', 20)

for i=1:size(M,1)
    plot([SomaLocations(i,1)/scaleFactor synapses(i,[1])/scaleFactor],[-SomaLocations(i,2)/scaleFactor -synapses(i,2)/scaleFactor],'color',[0 0 0 0.1])
end


% figure;plot3(SomaLocations(:,1),SomaLocations(:,2),SomaLocations(:,3),'ro')

D=pdist(SomaLocations);

Z=squareform(D);

% set
figure;histogram(D,500)
vline(1000,'r')
 xlabel('target paired distance, px')
ylabel ('count')

sameCellsD=D<1e3;
sameCellsZ=Z<1e3;


figure;

subaxis(1,2,1)
imagesc(Z)
title('postSynaptic cell pairwise distance')
subaxis(1,2,2)
imagesc(sameCellsZ)
title('Same cells')

% currentCellID=0;
% TargetCellID=nan(size(Z,1),size(Z,1));
% for synapseID=1:size(Z,1)-1
%     currentCellID=currentCellID+1; % label all targets in one row that are close to each other with the same cellID
%     for j=1:synapseID   %size(SomaLocations,1)
%         if sameCellsZ(synapseID,j)
%         TargetCellID(synapseID,j)=currentCellID;
% %         TargetCellID(j,synapseID)=currentCellID;
%         else
%             TargetCellID(synapseID,j)=nan;
%         end
%     end
% end

figure;imagesc(TargetCellID);
h = colorbar;
ylabel(h, 'PostsynapticID')
colormap('jet')
xlabel('synapseID')
ylabel('synapseID')

% go down each columm of the logical matrix, use idx to assign ID
%IN PROGRESS, VERIFY THAT IT WORKS BY PLOTTING EACH CELL WITH DIFFERENT
%COLOR IN 3D.
currentCellID=1
CellID=nan(size(sameCellsZ,1),1);
for col=1:size(sameCellsZ,1)
    
    for synapse=1:size(sameCellsZ,1)
        if sameCellsZ(synapse,col)
            
        if isnan(CellID(synapse))
            CellID(synapse)=currentCellID;
        else
        end
        end
    end
    currentCellID=max(CellID)+1
end
 
% figure;histogram(CellID,











[C,ia,ic] = unique(TargetCellID);
% Count the number of times each element in C appears in a. Specify ic as the first input to accumarray and 1 as the second input so that the function counts repeated subscripts in ic. Summarize the results.

a_counts = accumarray(ic,1);
value_counts = [C, a_counts]

%half, basically pair 1,200 has a different ID.

        

    